package com.enjoy.xbase.ui.load;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;


public abstract class BaseLoadView extends RelativeLayout implements ILoadView {

    //数据加载界面
    protected View loadView;

    //遮罩层数据加载视图
    protected View loadingView;

    //无数据视图
    protected View emptyView;

    //失败视图
    protected View failView;

    public BaseLoadView(Context context) {
        super(context);
    }

    public BaseLoadView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BaseLoadView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void showLoad() {
        hideAll(loadView);
        if (loadView == null) {
            if (loadView == null) {
                loadView = createLoadView(this);
                loadView.setVisibility(GONE);
            }
            addView(loadView);
        }
        if (loadView != null) {
            if (loadView.getVisibility()==GONE){
                onLoadActionStart();
            }
            loadView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showLoading() {
        hideAll(loadingView);
        if (loadingView == null) {
            if (loadingView == null) {
                loadingView = createLoadingView(this);
                loadingView.setVisibility(GONE);
            }
            addView(loadingView);
        }
        if (loadingView != null) {
            if (loadingView.getVisibility()==GONE){
                onLoadingActionStart();
            }
            loadingView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showFaill() {
        hideAll(failView);
        if (failView == null) {
            if (failView == null) {
                failView = createFaillView(this);
            }
            addView(failView);
        }
        if (failView != null) {
            failView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showEmpty() {
        hideAll(emptyView);
        if (emptyView == null) {
            if (emptyView == null) {
                emptyView = createEmptyView(this);
            }
            addView(emptyView);
        }
        if (emptyView != null) {
            emptyView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showData() {
        hideAll(null);
    }

    private void hideAll(View showView) {
        if (loadView != null) {
            if (loadView.getVisibility()==VISIBLE&&loadView!=showView){
                onLoadActionEnd();
            }
            loadView.setVisibility(View.GONE);
        }
        if (emptyView != null) {
            emptyView.setVisibility(View.GONE);
        }
        if (failView != null) {
            failView.setVisibility(View.GONE);
        }
        if (loadingView != null) {
            if (loadingView.getVisibility()==VISIBLE&&loadingView!=showView){
                onLoadingActionEnd();
            }
            loadingView.setVisibility(View.GONE);
        }
    }
    protected abstract View createLoadView(ViewGroup parent);

    protected abstract View createFaillView(ViewGroup parent);

    protected abstract View createEmptyView(ViewGroup parent);

    protected abstract View createLoadingView(ViewGroup parent);

    protected abstract int getLoadLayoutId();

    protected abstract int getFaillLayoutId();

    protected abstract int getEmptyLayoutId();

    protected abstract int getLoadingLayoutId();

    protected abstract void onLoadActionStart();

    protected abstract void onLoadActionEnd();

    protected abstract void onLoadingActionStart();

    protected abstract void onLoadingActionEnd();

}
